//package com.ev;
//
//import com.ev.anno.ExcelFieldName;
//import com.ev.entity.SummaryTable;
//import com.vividsolutions.jts.geom.Coordinate;
//import com.vividsolutions.jts.geom.Geometry;
//import com.vividsolutions.jts.geom.GeometryFactory;
//import com.vividsolutions.jts.geom.Point;
//import org.geotools.data.DefaultTransaction;
//import org.geotools.data.Transaction;
//import org.geotools.data.collection.ListFeatureCollection;
//import org.geotools.data.shapefile.ShapefileDataStore;
//import org.geotools.data.shapefile.ShapefileDataStoreFactory;
//import org.geotools.data.simple.SimpleFeatureCollection;
//import org.geotools.data.simple.SimpleFeatureSource;
//import org.geotools.data.simple.SimpleFeatureStore;
//import org.geotools.feature.simple.SimpleFeatureBuilder;
//import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
//import org.geotools.geometry.jts.JTSFactoryFinder;
//import org.geotools.referencing.crs.DefaultGeographicCRS;
//import org.geotools.swing.data.JFileDataStoreChooser;
//import org.opengis.feature.simple.SimpleFeature;
//import org.opengis.feature.simple.SimpleFeatureType;
//
//import java.io.File;
//import java.io.IOException;
//import java.io.Serializable;
//import java.lang.reflect.Field;
//import java.nio.charset.Charset;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @Description:
// * @Author: GuoShouFu
// * @Date: 2022/6/27
// **/
//public class GenericShp3 {
//
//    private static SimpleFeatureType createFeatureType(Class<? extends Geometry> geometryClazz) {
//        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
//        builder.setName("Location");
//        // <- Coordinate reference system
//        builder.setCRS(DefaultGeographicCRS.WGS84);
//
//        // add attributes in order
//        builder.add("the_geom", geometryClazz);
//        // <- 15 chars width for name field
//
//        String[] array = new String[]{"YEAR", "外业核查内容", "地块编号", "经度", "纬度", "DLMC", "DLDM", "是否变化", "GD", "YBD"
//                , "EMC", "EDM", "照片文件路径", "林分结构", "优势树草种", "乔木郁闭度", "灌草盖度", "林下综合盖度", "综合盖度"
//                , "坡度", "坡向", "株距", "行距", "冠幅", "生长周期", "梯田设计标准", "梯田类型", "田面宽度", "边坎类型", "梯田质量"
//                , "损毁程度", "开垦利用方式", "所处地貌类型区", "所在区域", "对应的项目部位", "水土流失治理度", "项目所处的建设阶段"
//                , "扰动方式", "QDLX", "QDDM", "BZ", "核查矢量编号", "检查", "TDLYDM_SL", "EDM_SL", "QD202153"};
//
////        String[] array = new String[]{"YEAR", "waiye", "地块编号", "经度", "纬度", "DLMC", "DLDM", "是否变化", "GD", "YBD"
////                , "EMC", "EDM", "照片文件路径", "林分结构", "优势树草种", "乔木郁闭度", "灌草盖度", "林下综合盖度", "综合盖度"
////                , "坡度", "坡向", "株距", "行距", "冠幅", "生长周期", "梯田设计标准", "梯田类型", "田面宽度", "边坎类型", "梯田质量"
////                , "损毁程度", "开垦利用方式", "所处地貌类型区", "所在区域", "对应的项目部位", "水土流失治理度", "项目所处的建设阶段"
////                , "扰动方式", "QDLX", "QDDM", "BZ", "核查矢量编号", "检查", "TDLYDM_SL", "EDM_SL", "QD202153"};
//        for (String s : array) {
//            builder.length(100).add(s, String.class);
//        }
//        // build the type
//        return builder.buildFeatureType();
//    }
//
//    /**
//     * xml文件的记录封装到SimpleFeature集合中
//     *
//     * @param type
//     * @param file
//     * @return
//     * @throws IOException
//     */
//    static List<SimpleFeature> readXls2Feature(SimpleFeatureType type, File file) throws Exception {
//        //A list to collect result as we create them.
//        List<SimpleFeature> result = new ArrayList<>();
//
//        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
//        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
//
//        ExcelReader reader = new ExcelReader(file);
//
//        List<List<String>> read = reader.read(0);
//        List<SummaryTable> summaryTableList = listStringConvert2Entity(read, SummaryTable.class);
//
//        for (SummaryTable summaryTable : summaryTableList) {
//            Point point = geometryFactory.createPoint(new Coordinate(Double.parseDouble(summaryTable.getX()), Double.parseDouble(summaryTable.getY())));
//            featureBuilder.add(point);
//            featureBuilder.add(summaryTable.getYear());
//            featureBuilder.add(summaryTable.getVerification());
//            featureBuilder.add(summaryTable.getLandNo());
//            featureBuilder.add(summaryTable.getLongitude());
//            featureBuilder.add(summaryTable.getLatitude());
//            featureBuilder.add(summaryTable.getDLMC());
//            featureBuilder.add(summaryTable.getDLDM());
//            featureBuilder.add(summaryTable.getGD());
//            featureBuilder.add(summaryTable.getYBD());
//            featureBuilder.add(summaryTable.getEMC());
//            featureBuilder.add(summaryTable.getEDM());
//            featureBuilder.add(summaryTable.getPhotoFilePath());
//            featureBuilder.add(summaryTable.getStandStructure());
//            featureBuilder.add(summaryTable.getDominantTreeAndGrassSpecies());
//            featureBuilder.add(summaryTable.getArborCanopyDensity());
//            featureBuilder.add(summaryTable.getShrubGrassCoverage());
//            featureBuilder.add(summaryTable.getUndergrowthComprehensiveCoverage());
//            featureBuilder.add(summaryTable.getComprehensiveVoverage());
//            featureBuilder.add(summaryTable.getSlope());
//            featureBuilder.add(summaryTable.getAspectOfSlope());
//            featureBuilder.add(summaryTable.getPlantSpacing());
//            featureBuilder.add(summaryTable.getRowSpacing());
//            featureBuilder.add(summaryTable.getCrownWidth());
//            featureBuilder.add(summaryTable.getTerraceDesignStandard());
//            featureBuilder.add(summaryTable.getTerraceType());
//            featureBuilder.add(summaryTable.getFieldWidth());
//            featureBuilder.add(summaryTable.getEdgeSillType());
//            featureBuilder.add(summaryTable.getTerraceQuality());
//            featureBuilder.add(summaryTable.getDamageDegree());
//            featureBuilder.add(summaryTable.getUtilizationMode());
//            featureBuilder.add(summaryTable.getAreaType());
//            featureBuilder.add(summaryTable.getArea());
//            featureBuilder.add(summaryTable.getProjectLocation());
//            featureBuilder.add(summaryTable.getWaterAndSoilLoss());
//            featureBuilder.add(summaryTable.getDisturbanceMode());
//            featureBuilder.add(summaryTable.getQDLX());
//            featureBuilder.add(summaryTable.getQDDM());
//            featureBuilder.add(summaryTable.getBZ());
//            featureBuilder.add(summaryTable.getVerifyVectorNumber());
//            featureBuilder.add(summaryTable.getInspect());
//            featureBuilder.add(summaryTable.getInspect());
//            featureBuilder.add(summaryTable.getTDLYDM_SL());
//            featureBuilder.add(summaryTable.getEDM_SL());
//            featureBuilder.add(summaryTable.getQD202153());
//            SimpleFeature feature = featureBuilder.buildFeature(summaryTable.getId());
//            result.add(feature);
//        }
//
//        return result;
//    }
//
//    static <T> List<T> listStringConvert2Entity(List<List<String>> read, Class<T> clazz) throws Exception {
//        List<String> excelFieldList = read.get(0);
//        Field[] fields = clazz.getDeclaredFields();
//
//        List<T> rusult = new ArrayList<>();
//        for (int i = 1; i < read.size(); i++) {
//            List<String> rowList = read.get(i);
//            T instance = clazz.newInstance();
//            for (Field field : fields) {
//                field.setAccessible(true);
//                if (field.isAnnotationPresent(ExcelFieldName.class)) {
//                    String value = field.getAnnotation(ExcelFieldName.class).value();
//                    Integer columnIndex = getColumnIndex(excelFieldList, value);
//                    field.set(instance, rowList.get(columnIndex));
//                }
//            }
//            rusult.add(instance);
//        }
//
//        return rusult;
//    }
//
//    private static Integer getColumnIndex(List<String> excelFieldList, String value) {
//        for (int i = 0; i < excelFieldList.size(); i++) {
//            if (value.equals(excelFieldList.get(i))) {
//                return i;
//            }
//        }
//        throw new RuntimeException("数据异常");
//    }
//
//    /**
//     * 在与xml文件同级目录下创建shp文件
//     *
//     * @param csvFile
//     * @return
//     */
//    private static File getNewShapeFile(File csvFile) {
//        String path = csvFile.getAbsolutePath();
//        String newPath = path.substring(0, path.length() - 4) + ".shp";
//
//        JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
//        chooser.setDialogTitle("Save shapefile");
//        chooser.setSelectedFile(new File(newPath));
//
//        int returnVal = chooser.showSaveDialog(null);
//
//        if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) {
//            // the user cancelled the dialog
//            System.exit(0);
//        }
//        File newFile = chooser.getSelectedFile();
//        if (newFile.equals(csvFile)) {
//            System.out.println("Error: cannot replace " + csvFile);
//            System.exit(0);
//        }
//        return newFile;
//    }
//
//    public static void main(String[] args) throws Exception {
//
//
//
//
//
//
//
//
//
//        File outFile = new File("C:\\Users\\55072\\Desktop\\新建文件夹\\汇总表.shp");
//        Map<String, Serializable> params = new HashMap<>();
//        params.put(ShapefileDataStoreFactory.URLP.key, outFile.toURI().toURL());
//
//        ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(params);
//
//        // ds.setCharset(Charset.forName("UTF-8"));
//        SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
//        tb.setCRS(DefaultGeographicCRS.WGS84);
//        tb.setName("shapefile");
//        // add attributes in order
//        tb.add("the_geom", Point.class);
//        // <- 15 chars width for name field
//
//        String[] array = new String[]{"YEAR", "外业核查内容", "地块编号", "经度", "纬度", "DLMC", "DLDM", "是否变化", "GD", "YBD"
//                , "EMC", "EDM", "照片文件路径", "林分结构", "优势树草种", "乔木郁闭度", "灌草盖度", "林下综合盖度", "综合盖度"
//                , "坡度", "坡向", "株距", "行距", "冠幅", "生长周期", "梯田设计标准", "梯田类型", "田面宽度", "边坎类型", "梯田质量"
//                , "损毁程度", "开垦利用方式", "所处地貌类型区", "所在区域", "对应的项目部位", "水土流失治理度", "项目所处的建设阶段"
//                , "扰动方式", "QDLX", "QDDM", "BZ", "核查矢量编号", "检查", "TDLYDM_SL", "EDM_SL", "QD202153"};
//
//        for (String s : array) {
//            tb.length(100).add(s, String.class);
//        }
//        SimpleFeatureType type = tb.buildFeatureType();
//        ds.createSchema(type);
//        ds.setCharset(Charset.forName("GBK"));
//
//
//
//
//
//
//
//
//
//
//        File file = new File("D:\\记录\\华油\\测试数据（表格+照片）\\测试数据（表格+照片）\\汇总表格-测试数据改.xls");
//
//        //Create a FeatureType
//        SimpleFeatureType type = createFeatureType(Point.class);
//
//        //将xml转换为feature
//        List<SimpleFeature> features = readXls2Feature(type, file);
//
//        //Get an output file name and create the new shapefile
//        File newFile = getNewShapeFile(file);
//
//        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
//
//        Map<String, Serializable> params = new HashMap<>(2);
//        params.put("url", newFile.toURI().toURL());
//        params.put("create spatial index", Boolean.TRUE);
//
//        ShapefileDataStore newDataStore =
//                (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
//
////        newDataStore.setCharset(Charset.forName("UTF-8"));
//        newDataStore.setCharset(Charset.forName("GBK"));
//        //TYPE is used as a template to describe the file contents
//        newDataStore.createSchema(type);
//
//        //Write the features to the shapefile
//        Transaction transaction = new DefaultTransaction("create");
//
//        String typeName = newDataStore.getTypeNames()[0];
//        SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
//
//        if (featureSource instanceof SimpleFeatureStore) {
//            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
//            SimpleFeatureCollection collection = new ListFeatureCollection(type, features);
//            featureStore.setTransaction(transaction);
//            try {
//                featureStore.addFeatures(collection);
//                transaction.commit();
//            } catch (Exception problem) {
//                problem.printStackTrace();
//                transaction.rollback();
//            } finally {
//                transaction.close();
//            }
//            System.exit(0);
//        } else {
//            System.out.println(typeName + " does not support read/write access");
//            System.exit(1);
//        }
//    }
//
//
//}
